package com.fastjrun.bms.service.impl;

import com.fastjrun.bms.dao.PressDao;
import com.fastjrun.bms.dto.PressDTO;
import com.fastjrun.bms.dto.PressQueryDTO;
import com.fastjrun.bms.entity.Press;
import com.fastjrun.bms.entity.PressExample;
import com.fastjrun.bms.entity.SysUser;
import com.fastjrun.bms.entity.SysUserExample;
import com.fastjrun.bms.helper.DTOToEntifyConverter;
import com.fastjrun.bms.helper.EntityToVOConverter;
import com.fastjrun.bms.service.BaseDemoService;
import com.fastjrun.bms.service.PressManagerService;
import com.fastjrun.bms.vo.PressVO;
import com.fastjrun.bms.vo.TotalVO;
import com.fastjrun.bms.vo.UserVO;
import com.fastjrun.dto.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目的 @Copyright 2021 快嘉. All rights reserved.
 *
 * @author cuiyingfeng
 */
@Service("pressManagerService")
public class PressManagerServiceImpl extends BaseDemoService implements PressManagerService {
  @Autowired PressDao pressDao;

  /** 条件分页查询出版社列表 */
  @Override
  public PageResult<PressVO> search(PressQueryDTO requestBody) {
    PageResult<PressVO> response = new PageResult();

    String name = requestBody.getName();
    Integer current = requestBody.getCurrent();
    Integer size = requestBody.getSize();
    if (current == null) {
      current = PAGE_CURRENT;
    }
    if (size == null) {
      size = PAGE_SIZE;
    }

    PressExample pressExample = new PressExample();

    if (name != null && !name.equals("")) {
      pressExample.createCriteria().andNameLike("%" + name + "%");
    }
    PageHelper.startPage(current, size, true);

    PageInfo<Press> pageInfo = new PageInfo<>(pressDao.selectByExample(pressExample));

    List<PressVO> list =
            pageInfo.getList().stream().map(EntityToVOConverter::convert).collect(Collectors.toList());

    response.setRows(list);
    response.setCurrPage(current);
    response.setPageSize(size);
    response.setTotalPage(pageInfo.getPages());
    response.setTotal((int) pageInfo.getTotal());
    return response;
  }

  /** 查询出版社信息 */
  @Override
  public PressVO getById(Long id) {
    Press press=pressDao.selectByPrimaryKey(id);
    return EntityToVOConverter.convert(press);
  }

  /** 新增出版社 */
  @Override
  public void add(PressDTO requestBody) {
    Press press= DTOToEntifyConverter.convert(requestBody);
    Date currentDate = new Date(System.currentTimeMillis());
    press.setCreateDate(currentDate);
    press.setUpdateDate(currentDate);
    pressDao.insert(press);
  }

  /** 修改出版社 */
  @Override
  public void save(PressDTO requestBody) {
    Press press= DTOToEntifyConverter.convert(requestBody);
    Date currentDate = new Date(System.currentTimeMillis());
    press.setUpdateDate(currentDate);
    pressDao.updateByPrimaryKeySelective(press);
  }

  /** 删除出版社信息 */
  @Override
  public void deleteById(Long id) {
    pressDao.deleteByPrimaryKey(id);
  }
}
